﻿<?php
Class Incident{
	private $dbh;
	private $id;
	private $machine;
	private $type;
	private $complement;
	private $date_detection;
	private $date_resolution;
	private $is_resolved;
	
	/*Constructeur. Il va aller chercher l'utilisateur correspondant à l'ID et remplir les paramètres de l'objet avec les données de la BDD*/
	public function __construct($dbh, $id=NULL)
	{
	
		if($dbh != NULL){
			$this->dbh = $dbh;
			if($id != NULL){
				$stmt = $this->dbh->prepare("SELECT id_incident, machine, type, complement, date_detection, date_resolution FROM incident_bliss where id_incident=:id");
				$stmt->bindParam(':id', $id);//:variable traitement de la variable APRES
				if($stmt->execute()){
					while ($row = $stmt->fetch()){
						$this->id = $row['id_incident'];
						$this->machine = new machine($dbh, $row['machine']);
						$this->type = new Type_incident($dbh, $row['type']);
						$this->complement = $row['complement'];
						$this->date_detection = $row['date_detection'];
						$this->date_resolution = $row['date_resolution'];
						if($row['date_resolution'] == '0000-00-00 00:00:00'){
							$this->is_resolved = false;
						}else{
							$this->is_resolved = true;
						}
					}
				}
			}else{
				$this->id = NULL;
				$this->machine = new machine($dbh, NULL);
				$this->type = new Type_incident($dbh, NULL);
				$this->complement = NULL;
				$this->date_detection = NULL;
				$this->date_resolution = NULL;
				$this->is_resolved = false;
			}
			
		}
	}
	
	/*Getters et Setters pour chaque paramètre*/
	public function getId(){
		return $this->id;
	}
	
	public function setId($id){
		$this->id = $id;
	}

	public function getMachine(){
		return $this->machine;
	}

	public function setMachine($machine){
		$this->machine = new machine ($this->dbh, $machine);
	}

	public function getType(){
		return $this->type;
	}

	public function setType($type){
		$this->type = new Type_incident ($this->dbh, $type);
	}

	public function getComplement(){
	 return $this->complement;
	}

	public function setComplement($complement){
		$this->complement = $complement;
	}	

	public function getDate_detection(){
		return $this->date_detection;
	}

	public function setDate_detection($date_detection){
		$this->date_detection = $date_detection;
	}

	public function getDate_resolution(){
		return $this->date_resolution;
	}

	public function setDate_resolution($date_resolution){
		$this->date_resolution = $date_resolution;
	}
	
	public function getIs_resolved(){
		$this->is_resolved;
	}
	
	public function setIs_resolved($is_resolved){
		$this->is_resolved = $is_resolved;
	}

	/*Fonction de sauvegarde*/
	public function save(){
		if($this->id != NULL){
			if($this->is_resolved == 'true'){
				$stmt = $this->dbh->prepare("UPDATE `incident_bliss` SET machine=:machine, type=:type, complement=:complement, date_resolution=NOW() where id_incident=:id");
			}else{
				$stmt = $this->dbh->prepare("UPDATE `incident_bliss` SET machine=:machine, type=:type, complement=:complement, date_resolution='0000-00-00 00:00:00' where id_incident=:id");
			} 
			$stmt->bindParam(':machine', $idM);
			$idM= $this->machine->getId();
			$stmt->bindParam(':type', $idTyp);
			$idTyp= $this->type->getId();
			$stmt->bindParam(':complement', $this->complement);
			$stmt->bindParam(':id', $this->id);
			
			$ret = $stmt->execute();
		}
		elseif($this->checkParam($this->machine) && $this->checkParam($this->type)){
			if($this->is_resolved == 'true'){
				$stmt = $this->dbh->prepare("INSERT INTO `incident_bliss` SET machine=:machine, type=:type, complement=:complement, date_resolution=NOW()");
			}else{
				$stmt = $this->dbh->prepare("INSERT INTO `incident_bliss` SET machine=:machine, type=:type, complement=:complement, date_resolution='0000-00-00 00:00:00'");
			} 
			
			$stmt->bindParam(':machine', $idmachine);
			$idmachine = $this->machine->getId();
			$stmt->bindParam(':type', $idtype);
			$idtype = $this->type->getId();
			$stmt->bindParam(':complement', $this->complement);
			
			$ret = $stmt->execute();
			$this->id = $this->dbh->lastInsertId();
		}
		return $ret;
		
	}
	/*Fonction de suppression*/
	public function delete(){
		if($this->id != NULL){
			$stmt = $this->dbh->prepare("DELETE FROM `incident_bliss` WHERE id_incident=:id");
			$stmt->bindParam(':id', $this->id);
			
			$ret = $stmt->execute();
			return true;
		}
	}
	
	/*Fonction pour verifier que le parametre en argument n'est pas vide*/
	private function checkParam($param){
		if($param == NULL || $param == '')
			return false;
		else
			return true;
	}
}